<!DOCTYPE html>
<html lang="en" xmlns:th="https://www.thymeleaf.org">

<head>
    <meta charset="utf-8">
    <title>
        定时任务添加编辑页面
    </title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="format-detection" content="telephone=no">
    <link rel="stylesheet" th:href="@{/assets/admin/css/font.css}">
    <link rel="stylesheet" th:href="@{/assets/admin/css/xadmin.css}">
    <script th:src="@{/assets/admin/lib/layui/layui.js}" charset="utf-8"></script>
    <script type="text/javascript" th:src="@{/assets/admin/js/xadmin.js}"></script>
    <!--[if lt IE 9]>
    <script src="https://cdn.staticfile.org/html5shiv/r29/html5.min.js"></script>
    <script src="https://cdn.staticfile.org/respond.js/1.4.2/respond.min.js"></script>
    <![endif]-->
    <style type="text/css">
        .layui-form-switch {
            margin-top: 8px;
        }


    </style>
</head>

<body>
<div class="layui-fluid">
    <div class="layui-row layui-col-space15">
        <div class="layui-col-md12">
            <div class="layui-card">
                <div class="layui-card-body">
                    <form class="layui-form layui-col-space5" style="max-width: 770px;margin:0 auto;"> <!-- 提示：如果你不想用form，你可以换成div等任何一个普通元素 -->
                        <input type="hidden" name="jobId" th:value="${task == null?'':task.jobId}"/>
                        <div class="layui-inline layui-show-xs-block">
                            <label class="layui-form-label">
                                <span class="x-red">*</span>任务名
                            </label>
                            <div class="layui-input-inline">
                                <input type="text" th:disabled="${task !=null}" th:value="${task == null?'':task.jobName}" name="jobName"  lay-verify="required" placeholder="请输入任务名" autocomplete="off" class="layui-input" id="jobName">
                            </div>
                        </div>
                        <div class="layui-inline layui-show-xs-block">
                            <label class="layui-form-label">
                                <span class="x-red">*</span>分组名
                            </label>
                            <div class="layui-inline layui-show-xs-block">
                                <select id="jobGroup" name="jobGroup" lay-verify="required" >
                                    <option vlaue="">---请选择组名---</option>
                                </select>
                            </div>
                        </div>
                        <div class="layui-inline layui-show-xs-block">
                            <label class="layui-form-label">
                                <span class="x-red">*</span>表达式
                            </label>
                            <div class="layui-inline layui-show-xs-block">
                                <input type="text" th:value="${task == null?'':task.cron}" id="cron" name="cron" lay-verify="required" placeholder="请输入表达式（cron）" autocomplete="off" class="layui-input">
                            </div>
                        </div>
                        <div class="layui-inline layui-show-xs-block" th:if="${task == null}">
                            <label class="layui-form-label">
                                <span class="x-red">*</span>类名
                            </label>
                            <div class="layui-input-inline">
                                <input type="text" name="beanName" id="beanName" lay-verify="required" placeholder="请输入类名" autocomplete="off" class="layui-input">
                            </div>
                        </div>
                        <div class="layui-inline layui-show-xs-block" th:if="${task == null}">
                            <label class="layui-form-label">
                                <span class="x-red">*</span>方法名
                            </label>
                            <div class="layui-input-inline">
                                <input type="methodName" name="methodName" lay-verify="required" placeholder="请输入方法名" autocomplete="off" class="layui-input" id="methodName">
                            </div>
                        </div>
                        <div class="layui-inline layui-show-xs-block">
                            <label class="layui-form-label">自启动</label>
                            <div class="layui-input-block">
                                <input type="checkbox" name="status" id="status" lay-skin="switch" lay-text="是|否"
                                       lay-filter="switchTest" th:checked="${task!= null and task.status =='1'}">
                            </div>
                        </div>
                        <br/>
                        <div class="layui-form-item" style="text-align: center">
                            <button class="layui-btn" lay-submit lay-filter="add" th:if="${task == null}">立即提交</button>
                            <button class="layui-btn" lay-submit lay-filter="update" th:if="${task != null}">更新信息</button>
                            <button type="reset" class="layui-btn layui-btn-primary">重置</button>
                        </div>
                    </form>
                </div>
            </div>
        </div>
    </div>
</div>
<script th:replace="systemCommon/HarinCommon :: commonJs"></script>
<script th:src="@{/assets/admin/js/jquery.min.js}"></script>
<script>
   $(function(){
       layui.config({
           base: '/assets/admin/lib/layui/mods/'
       }).extend({
           cron: 'cron/cron',
       });

       layui.use(['cron','form'], function () {
           cron = layui.cron;
           cron.render({
               elem: "#cron", // 绑定元素
               url:"https://www.fastmock.site/mock/58329c0ee61b03d6ce3ba0c1e2f13691/world/cron", // 获取最近运行时间的接口
               value: $("#cron").val(), // 默认值
               done: function (cronStr) {
                   $("#cron").val(cronStr);
               },
           });
           form = layui.form;
            //校验
           // form.verify({
           //     jobName : function(value,item){ //value：表单的值、item：表单的DOM对象
           //         if(isEmpty(value)){
           //             return "任务名不能为空";
           //         }
           //     },
           // });
           //新增用户信息
           form.on('submit(add)',function(obj){
               //console.log(obj.field);
               if(obj.field.status == "on"){
                   obj.field.status = "1";
               }else {
                   obj.field.status = "0";
               }
                $.ajax({
                    url: "/task/addJob",
                    type:"post",
                    contentType: 'application/json',
                    data:JSON.stringify(obj.field),
                    success:function(data){
                        if (data.code == 200) {
                            layer.alert(data.message, {icon: 6}, function (iIndex) {
                                // 获得frame索引
                                var index = parent.layer.getFrameIndex(window.name);
                                //关闭当前frame
                                parent.layer.close(index);
                            });
                        } else {
                            layer.alert(data.message, {icon: 5}, function (iIndex) {
                                // 获得frame索引
                                var index = parent.layer.getFrameIndex(window.name);
                                //关闭当前frame
                                //parent.layer.close(index);
                                layer.close(iIndex);
                            });
                        }
                    }
                })
               return false;
           });

           //新增用户信息
           form.on('submit(update)',function(obj){
               let  status = obj.field.status;
               if(status == "on"){
                   obj.field.status = "1";
               }else{
                   obj.field.status = "0";
               }
               $.ajax({
                   url: "/task/update",
                   type:"post",
                   contentType: 'application/json',
                   data:JSON.stringify(obj.field),
                   success:function(data){
                       if (data.code == 200) {
                           layer.alert(data.message, {icon: 6}, function (iIndex) {
                               // 获得frame索引
                               var index = parent.layer.getFrameIndex(window.name);
                               //关闭当前frame
                               parent.layer.close(index);
                           });
                       } else {
                           layer.alert(data.message, {icon: 5}, function (iIndex) {
                               // 获得frame索引
                               var index = parent.layer.getFrameIndex(window.name);
                               //关闭当前frame
                               //parent.layer.close(index);
                               layer.close(iIndex);
                           });
                       }
                   }
               })
               return false;
           });
       });
   });

</script>
</body>
</html>